package com.wjw.service;

import com.wjw.mapper.AncillaryFacilitiesTableMapper;
import com.wjw.model.Ancillary_facilities_table;
import com.wjw.utils.GetSqlSessionUtils;
import com.wjw.viewmodel.Ancillary_facilities_table_view;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AncillaryFacilitiesTableService {

//    private SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
//    private AncillaryFacilitiesTableMapper mapper=sqlsession.getMapper(AncillaryFacilitiesTableMapper.class);

    public List<Ancillary_facilities_table_view> findAll(){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        AncillaryFacilitiesTableMapper mapper=sqlsession.getMapper(AncillaryFacilitiesTableMapper.class);
        try {
            List<Ancillary_facilities_table> all = mapper.findAll();
            List<Ancillary_facilities_table_view> list=new ArrayList<Ancillary_facilities_table_view>();
            for(Ancillary_facilities_table e:all){
                Ancillary_facilities_table_view k=new Ancillary_facilities_table_view();
                k.setAncillary_check_num(e.getAncillary_check_num());
                k.setAncillary_facilities_brand_num(e.getAncillary_facilities_brand_num());
                k.setAncillary_facilities_id(e.getAncillary_facilities_id());
                k.setAncillary_facilities_other(e.getAncillary_facilities_other());
                k.setAncillary_facilities_sign_num(e.getAncillary_facilities_sign_num());
                k.setAncillary_facilities_tree_area(e.getAncillary_facilities_tree_area());
                k.setAncillary_rainwater_num(e.getAncillary_rainwater_num());
                k.setRoad_basic_name(mapper.findRoadById(e.getRoad_basic_id()).getRoad_basic_name());
                list.add(k);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    public List<Ancillary_facilities_table_view> findByPage(int page, int limit){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        AncillaryFacilitiesTableMapper mapper=sqlsession.getMapper(AncillaryFacilitiesTableMapper.class);
        try {
            int start=(page-1)*limit;
            List<Ancillary_facilities_table> all = mapper.findByPage(start,limit);
            List<Ancillary_facilities_table_view> list=new ArrayList<Ancillary_facilities_table_view>();
            for(Ancillary_facilities_table e:all){
                Ancillary_facilities_table_view k=new Ancillary_facilities_table_view();
                k.setAncillary_check_num(e.getAncillary_check_num());
                k.setAncillary_facilities_brand_num(e.getAncillary_facilities_brand_num());
                k.setAncillary_facilities_id(e.getAncillary_facilities_id());
                k.setAncillary_facilities_other(e.getAncillary_facilities_other());
                k.setAncillary_facilities_sign_num(e.getAncillary_facilities_sign_num());
                k.setAncillary_facilities_tree_area(e.getAncillary_facilities_tree_area());
                k.setAncillary_rainwater_num(e.getAncillary_rainwater_num());
                k.setRoad_basic_name(mapper.findRoadById(e.getRoad_basic_id()).getRoad_basic_name());
                list.add(k);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally {
            sqlsession.close();
        }
    }

    public boolean add(Ancillary_facilities_table_view e){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        AncillaryFacilitiesTableMapper mapper=sqlsession.getMapper(AncillaryFacilitiesTableMapper.class);
        try {
            Ancillary_facilities_table k=new Ancillary_facilities_table();
            k.setAncillary_check_num(e.getAncillary_check_num());
            k.setAncillary_facilities_brand_num(e.getAncillary_facilities_brand_num());
            k.setAncillary_facilities_id(e.getAncillary_facilities_id());
            k.setAncillary_facilities_other(e.getAncillary_facilities_other());
            k.setAncillary_facilities_sign_num(e.getAncillary_facilities_sign_num());
            k.setAncillary_facilities_tree_area(e.getAncillary_facilities_tree_area());
            k.setAncillary_rainwater_num(e.getAncillary_rainwater_num());
            k.setRoad_basic_id(mapper.findRoadByName(e.getRoad_basic_name()).getRoad_basic_id());
            mapper.add(k);
            sqlsession.commit();
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    public boolean delete(int id){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        AncillaryFacilitiesTableMapper mapper=sqlsession.getMapper(AncillaryFacilitiesTableMapper.class);
        try {
            mapper.delete(id);
            sqlsession.commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

    public boolean change(Ancillary_facilities_table_view e){
        SqlSession sqlsession= GetSqlSessionUtils.getSqlSession();
        AncillaryFacilitiesTableMapper mapper=sqlsession.getMapper(AncillaryFacilitiesTableMapper.class);
        try {
            Ancillary_facilities_table k=new Ancillary_facilities_table();
            k.setAncillary_check_num(e.getAncillary_check_num());
            k.setAncillary_facilities_brand_num(e.getAncillary_facilities_brand_num());
            k.setAncillary_facilities_id(e.getAncillary_facilities_id());
            k.setAncillary_facilities_other(e.getAncillary_facilities_other());
            k.setAncillary_facilities_sign_num(e.getAncillary_facilities_sign_num());
            k.setAncillary_facilities_tree_area(e.getAncillary_facilities_tree_area());
            k.setAncillary_rainwater_num(e.getAncillary_rainwater_num());
            k.setRoad_basic_id(mapper.findRoadByName(e.getRoad_basic_name()).getRoad_basic_id());
            mapper.change(k);
            sqlsession.commit();
            return true;
        } catch (Exception e1) {
            e1.printStackTrace();
            return false;
        }finally {
            sqlsession.close();
        }
    }

}
